package com.zhoug.zmui.input;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.zhoug.common.view.edit.ZMUIEditText;
import com.zhoug.common.view.edit.ZMUITextWatcher;

import androidx.annotation.Nullable;

/**
 * 键值对输入框
 * @Author: zhoug
 * @Date: 2024-07-08
 * @Description:
 */
public class ZMUIInputEditText extends ZMUIInputTextView{
    private boolean mCallValueChange =true;

    public ZMUIInputEditText(Context context) {

        super(context);
    }

    public ZMUIInputEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ZMUIInputEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected View createValueView(ViewGroup parent) {
        if(mValueView==null){
            ZMUIEditText editText = new ZMUIEditText(getContext());
            //去掉默认背景
            editText.setBackground(null);
            //去掉默认padding
            editText.setPadding(0, 0, 0, 0);
            editText.addTextChangedListener(new ZMUITextWatcher() {
                @Override
                public void afterTextChanged(View view, Editable s) {
                    if(isRequired() && mCallValueChange && isAutoValidateRequired()){
                        //必填验证
                        setError(s == null || s.toString().trim().isEmpty());
                    }
                }
            });
            mValueView=editText;
        }
        return mValueView;
    }

    @Override
    public void setValue(CharSequence value) {
        mCallValueChange =false;
        super.setValue(value);
        mCallValueChange =true;
    }

    public void setValue(CharSequence value, boolean callTextChange) {
        mCallValueChange =callTextChange;
        super.setValue(value);
        mCallValueChange =true;
    }

    @Override
    public ZMUIEditText getValueView() {
        return (ZMUIEditText) mValueView;
    }


}
